﻿using DrawLibrary.Adorners;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

/*1.静态方法获取装饰层：AdornerLayer.GetAdornerLayer*/
/*2.Add方法添加装饰，可添加多个装饰layer.Add(new QuadrangleAdorner(SI_JIAO));*/
/*3.GetAdorners，目前无法直接移除指定装饰layer.GetAdorners
 * 3.1要移除指定装饰可以起个名
 */

/*Label没有装饰层*/
namespace WPF01.Views
{
    /// <summary>
    /// Interaction logic for _Adorner
    /// </summary>
    public partial class _Adorner : UserControl
    {
        public _Adorner()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Loaded事件只能获取到TabControl内控件的Text属性，不能获取到Layer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var text = SHUI_YIN.Text;
            var layer = AdornerLayer.GetAdornerLayer(SHUI_YIN);
            //TextChange(SHUI_YIN);

        }
        private void InitCrossAdorner()
        {
            var layer = AdornerLayer.GetAdornerLayer(SHI_Zi_XIAN);
            layer.Add(new CrossAdorner(SHI_Zi_XIAN));
        }
        const string dornerName = "dr1";

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var layer = AdornerLayer.GetAdornerLayer(SI_JIAO);
            var dorner = new QuadrangleAdorner(SI_JIAO);
            dorner.Name = dornerName;
            layer.Add(dorner);
        }

        private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            var layer = AdornerLayer.GetAdornerLayer(SI_JIAO);
            var adorners = layer.GetAdorners(SI_JIAO);
            if (adorners != null)
            {
                for (int i = adorners.Length - 1; i >= 0; i--)
                {
                    if (adorners[i].Name == dornerName)
                    {
                        layer.Remove(adorners[i]);
                    }
                }
            }
        }
   
        private void SHI_Zi_XIAN_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InitCrossAdorner();
        }

        private void SHI_Zi_XIAN_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ClearAdorner(SHI_Zi_XIAN);
        }

        private void ClearAdorner(UIElement element)
        {
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(element);
            Adorner[] adorners = adornerLayer.GetAdorners(element);
            if (adorners != null)
            {
                for (int i = adorners.Length - 1; i >= 0; i--)
                {
                    adornerLayer.Remove(adorners[i]);
                }
            }
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl tabControl = sender as TabControl;
            if (tabControl != null && tabControl.SelectedItem != null)
            {
                TabItem selectedTab = tabControl.SelectedItem as TabItem;
                if ((string)selectedTab.Header == "文本框水印")
                {
                    var layer = AdornerLayer.GetAdornerLayer(SHUI_YIN);
                    layer.Add(new PlaceholderAdorner(SHUI_YIN, "请输入..."));
                }
                else
                {
                    ClearAdorner(SHUI_YIN);
                }
            }
        }
    }
}
